home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 1.iso / dist / fw_mysql.idb / usr / freeware / share / sql-bench / bench-init.pl.z / bench-init.pl
Encoding:
Perl Script  |  1999-10-18  |  11.3 KB  |  471 lines

  1. #!/bin/perl5
  2. ##########################################################
  3. # this is the base file every test is using ....
  4. # this is made for not changing every file if we want to
  5. # add an option or just want to change something in
  6. # code what is the same in every file ...
  7. ##########################################################
  8.  
  9. #
  10. # The exported values are:
  11.  
  12. # $opt_...    Various options
  13. # $date        Current date in ISO format
  14. # $server    Object for current server
  15. # $limits    Hash reference to limits for benchmark
  16.  
  17. $benchmark_version="2.0b";
  18. use Getopt::Long;
  19.  
  20. require "$pwd/server-cfg" || die "Can't read Configuration file: $!\n";
  21.  
  22. $|=1;                # Output data immediately
  23.  
  24. $opt_skip_test=$opt_skip_create=$opt_skip_delete=$opt_verbose=$opt_fast_insert=$opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=$opt_log=$opt_use_old_results=$opt_help=$opt_odbc=$opt_small_test=$opt_small_tables=$opt_samll_key_tables=$opt_stage=$opt_old_headers=0;
  25. $opt_cmp=$opt_user=$opt_password="";
  26. $opt_server="mysql"; $opt_dir="output";
  27. $opt_host="localhost";$opt_database="test";
  28. $opt_machine=""; $opt_suffix="";
  29.  
  30. $opt_time_limit=10*60;        # Don't wait more than 10 min for some tests
  31.  
  32. $log_prog_args=join(" ", skip_arguments(\@ARGV,"comments","cmp","server",
  33.                     "user", "host", "database", "password",
  34.                     "use-old-results","skip-test",
  35.                     "machine", "dir", "suffix", "log"));
  36. GetOptions("skip-test=s","comments=s","cmp=s","server=s","user=s","host=s","database=s","password=s","loop-count=i","row-count=i","skip-create","skip-delete","verbose","fast-insert","lock-tables","debug","fast","force","field-count=i","regions=i","groups=i","time-limit=i","log","use-old-results","machine=s","dir=s","suffix=s","help","odbc","small-test","small-tables","small-key-tables","stage=i","old-headers") || usage();
  37.  
  38. usage() if ($opt_help);
  39. $server=get_server($opt_server,$opt_host,$opt_database,$opt_odbc);
  40. $limits=merge_limits($server,$opt_cmp);
  41. $date=date();
  42. @estimated=(0.0,0.0,0.0);        # For estimated time support
  43.  
  44. if (length($opt_cmp) && index($opt_cmp,$opt_server) < 0)
  45. {
  46.   my $tmp= $opt_server;
  47.   $tmp =~ s/_odbc$//;
  48.   $opt_cmp.=",$tmp";
  49. }
  50. $opt_cmp=lc(join(",",sort(split(',',$opt_cmp))));
  51.  
  52. #
  53. # set opt_lock_tables if one uses --fast and drivers supports it
  54. #
  55.  
  56. if (($opt_lock_tables || $opt_fast) && $server->{'limits'}->{'lock_tables'})
  57. {
  58.   $opt_lock_tables=1;
  59. }
  60. else
  61. {
  62.   $opt_lock_tables=0;
  63. }
  64. if ($opt_fast)
  65. {
  66.   $opt_fast_insert=1;
  67.   $opt_suffix="_fast" if (!length($opt_suffix));
  68. }
  69.  
  70. if ($opt_odbc)
  71. {
  72.    $opt_suffix="_odbc" if (!length($opt_suffix));
  73. }
  74.  
  75. if (!$opt_silent)
  76. {
  77.   print "Testing server '" . $server->version() . "' at $date\n\n";
  78. }
  79.  
  80. if ($opt_debug)
  81. {
  82.   print "\nCurrent limits: \n";
  83.   foreach $key (sort keys %$limits)
  84.   {
  85.     print $key . " " x (30-length($key)) . $limits->{$key} . "\n";
  86.   }
  87.   print "\n";
  88. }
  89.  
  90. #
  91. # Some help functions
  92. #
  93.  
  94. sub skip_arguments
  95. {
  96.   my($argv,@skip_args)=@_;
  97.   my($skip,$arg,$name,@res);
  98.  
  99.   foreach $arg (@$argv)
  100.   {
  101.     if ($arg =~ /^\-+([^=]*)/)
  102.     {
  103.       $name=$1;
  104.       foreach $skip (@skip_args)
  105.       {
  106.     if (index($skip,$name) == 0)
  107.     {
  108.       $name="";        # Don't use this parameters
  109.       last;
  110.     }
  111.       }
  112.       push (@res,$arg) if (length($name));
  113.     }
  114.   }
  115.   return @res;
  116. }
  117.  
  118.  
  119. sub merge_limits
  120. {
  121.   my ($server,$cmp)= @_;
  122.   my ($name,$tmp_server,$limits,$res_limits,$limit,$tmp_limits);
  123.  
  124.   $res_limits=$server->{'limits'};
  125.   if ($cmp)
  126.   {
  127.     foreach $name (split(",",$cmp))
  128.     {
  129.       $tmp_server= get_server($name,$opt_host, $opt_database,
  130.                   $opt_odbc) || die "Unknown SQL server: $name\n";
  131.       $limits=$tmp_server->{'limits'};
  132.       %new_limits=();
  133.       foreach $limit (keys(%$limits))
  134.       {
  135.     if (defined($res_limits->{$limit}) && defined($limits->{$limit}))
  136.     {
  137.       $new_limits{$limit}=min($res_limits->{$limit},$limits->{$limit});
  138.     }
  139.       }
  140.       %tmp_limits=%new_limits;
  141.       $res_limits=\%tmp_limits;
  142.     }
  143.   }
  144.   return $res_limits;
  145. }
  146.  
  147. sub date
  148. {
  149.   my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time());
  150.   sprintf("%04d-%02d-%02d %2d:%02d:%02d",
  151.       1900+$year,$mon+1,$mday,$hour,$min,$sec);
  152. }
  153.  
  154. sub min
  155. {
  156.   my($min)=$_[0];
  157.   my($i);
  158.   for ($i=1 ; $i <= $#_; $i++)
  159.   {
  160.     $min=$_[$i] if ($min > $_[$i]);
  161.   }
  162.   return $min;
  163. }
  164.  
  165. sub max
  166. {
  167.   my($max)=$_[0];
  168.   my($i);
  169.   for ($i=1 ; $i <= $#_; $i++)
  170.   {
  171.     $max=$_[$i] if ($max < $_[$i]);
  172.   }
  173.   return $max;
  174. }
  175.  
  176.  
  177. #
  178. # Execute many statements in a row
  179. #
  180.  
  181. sub do_many
  182. {
  183.   my ($dbh,@statements)=@_;
  184.   my ($statement,$sth);
  185.  
  186.   foreach $statement (@statements)
  187.   {
  188.     if (!($sth=$dbh->do($statement)))
  189.     {
  190.       die "Can't execute command '$statement'\nError: $DBI::errstr\n";
  191.     }
  192.   }
  193. }
  194.  
  195. #
  196. # Do a query and fetch all rows from a statement and return the number of rows
  197. #
  198.  
  199. sub fetch_all_rows
  200. {
  201.   my ($dbh,$query,$must_get_result)=@_;
  202.   my ($count,$sth);
  203.   $count=0;
  204.  
  205.   print "$query: " if ($opt_debug);
  206.   if (!($sth= $dbh->prepare($query)))
  207.   {
  208.     print "\n" if ($opt_debug);
  209.     die "Error occured with prepare($query)\n -> $DBI::errstr\n";
  210.     return undef;
  211.   }
  212.   if (!$sth->execute)
  213.   {
  214.     print "\n" if ($opt_debug);
  215.     die "Error occured with execute($query)\n -> $DBI::errstr\n";
  216.     $sth->finish;
  217.     return undef;
  218.   }
  219.   while ($sth->fetchrow_arrayref)
  220.   {
  221.     $count++;
  222.   }
  223.   print "$count\n" if ($opt_debug);
  224.   if (defined($must_get_result) && $must_get_result && !$count)
  225.   {
  226.     die "Error: Query $query didn't return any rows\n";
  227.   }
  228.   $sth->finish;
  229.   undef($sth);
  230.   return $count;
  231. }
  232.  
  233. sub do_query
  234. {
  235.   my($dbh,$query)=@_;
  236.   print "$query\n" if ($opt_debug);
  237.   $dbh->do($query) or
  238.     die "\nError executing '$query':\n$DBI::errstr\n";
  239. }
  240.  
  241.  
  242. #
  243. # Handle estimated time of the server is too slow
  244. # Returns 0 if one should continue as normal
  245. #
  246.  
  247. sub predict_query_time
  248. {
  249.   my ($loop_time,$end_time,$count_ref,$loop,$loop_count)= @_;
  250.   my ($k,$tmp);
  251.  
  252.   if (($end_time->[0] - $loop_time->[0]) > $opt_time_limit)
  253.   {
  254.     # We can't wait until the SUN dies.  Try to predict the end time
  255.     if ($loop != $loop_count)
  256.     {
  257.       $tmp=($end_time->[0] - $loop_time->[0]);
  258.       print "Note: Query took longer then time-limit: $opt_time_limit\nEstimating end time based on:\n";
  259.       print "$$count_ref queries in $loop loops of $loop_count loops took $tmp seconds\n";
  260.       for ($k=0; $k < 3; $k++)
  261.       {
  262.     $tmp=$loop_time->[$k]+($end_time->[$k]-$loop_time->[$k])/$loop*
  263.       $loop_count;
  264.     $estimated[$k]+=($tmp-$end_time->[$k]);
  265.     $end_time->[$k]=$tmp;
  266.       }
  267.       $$count_ref= int($$count_ref/$loop*$loop_count);
  268.       return 1;
  269.     }
  270.   }
  271.   return 0;
  272. }
  273.  
  274. #
  275. # standard end of benchmark
  276. #
  277.  
  278. sub end_benchmark
  279. {
  280.   my ($start_time)=@_;
  281.  
  282.   $end_time=new Benchmark;
  283.   if ($estimated[0])
  284.   {
  285.     print "Estimated total time: ";
  286.     $end_time->[0]+=$estimated[0];
  287.     $end_time->[1]+=$estimated[1];
  288.     $end_time->[2]+=$estimated[2];
  289.   }
  290.   else
  291.   {
  292.     print "Total time: "
  293.     }
  294.   print timestr(timediff($end_time, $start_time),"all") . "\n";
  295.   exit 0;
  296. }
  297.  
  298. #
  299. # Create a filename part for the machine that can be used for log file.
  300. #
  301.  
  302. sub machine_part
  303. {
  304.   my ($name);
  305.   return $opt_machine if (length($opt_machine)); # Specified by user
  306.   $name=machine();
  307.   $name="win9$1" if ($name =~ /win.*9(\d)/i);
  308.   $name="NT_$1" if ($name =~ /Windows NT.*(\d+\.\d+)/i);
  309.   $name =~ s/\s+/_/g;        # Make the filenames easier to parse
  310.   $name =~ s/-/_/g;
  311.   $name =~ s/\//_/g;
  312.   return $name;
  313. }
  314.  
  315. sub machine
  316. {
  317.   $name= `uname -s -r -m`;
  318.   if ($?)
  319.   {
  320.     $name= `uname -s -m`;
  321.   }
  322.   if ($?)
  323.   {
  324.     $name= `uname -s`;
  325.   }
  326.   if ($?)
  327.   {
  328.     $name= `uname`;
  329.   }
  330.   if ($?)
  331.   {
  332.     $name="unknown";
  333.   }
  334.   chomp($name); $name =~ s/[\n\r]//g;
  335.   return $name;
  336. }
  337.  
  338. #
  339. # Usage
  340. #
  341.  
  342. sub usage
  343. {
  344.     print <<EOF;
  345. The MySQL benchmarks Ver $benchmark_version
  346.  
  347. All benchmarks takes the following options:
  348.  
  349. --comments
  350.   Add a comment to the benchmark output.  Comments should contain
  351.   extra information that 'uname -a' doesn\'t give and if the database was
  352.   stared with some specific, non default, options.
  353.  
  354. --database (Default $opt_database)
  355.   In which database the test tables are created.
  356.  
  357. --debug
  358.   This is a test specific option that is only used when debugging a test.
  359.   Print out debugging information.
  360.  
  361. --dir (Default $opt_dir)
  362.   Option to 'run-all-tests' to where the test results should be stored.
  363.  
  364. --fast
  365.   Allow the database to use non standard ANSI SQL commands to make the
  366.   test go faster.
  367.  
  368. --fast-insert
  369.   Use "insert into table_name values(...)" instead of
  370.   "insert into table_name (....) values(...)"
  371.   If the database supports it, some tests uses multiple value lists.
  372.  
  373. --field-count
  374.   This is a test specific option that is only used when debugging a test.
  375.   This usually means how many fields there should be in the test table.
  376.  
  377. --force
  378.   This is a test specific option that is only used when debugging a test.
  379.   Continue the test even if there is some error.
  380.   Delete tables before creating new ones.
  381.  
  382. --groups (Default $opt_groups)
  383.   This is a test specific option that is only used when debugging a test.
  384.   This usually means how many different groups there should be in the test.
  385.  
  386. --lock-tables
  387.   Allow the database to use table locking to get more speed.
  388.  
  389. --log
  390.   Option to 'run-all-tests' to save the result to the '--dir' directory.
  391.  
  392. --loop-count (Default $opt_loop_count)
  393.   This is a test specific option that is only used when debugging a test.
  394.   This usually means how many times times each test loop is executed.
  395.  
  396. --help or --Information
  397.   Shows this help
  398.  
  399. --host='host name' (Default $opt_host)
  400.   Host name where the database server is located.
  401.  
  402. --machine="machine or os_name"
  403.   The machine/os name that is added to the benchmark output filename.
  404.   The default is the OS name + version.
  405.  
  406. --odbc
  407.   Use the ODBC DBI driver to connect to the database.
  408.  
  409. --password='password'
  410.   Password for the current user.
  411.  
  412. --regions
  413.   This is a test specific option that is only used when debugging a test.
  414.   This usually means how AND levels should be tested.
  415.  
  416. --old-headers
  417.   Get the old benchmark headers from the old RUN- file.
  418.  
  419. --server='server name'  (Default $opt_server)
  420.   Run the test on the given SQL server.
  421.   Known servers names are: Access, Adabas, AdabasD, Empress, Oracle,
  422.   Informix, DB2, mSQL, MS-SQL, MySQL, Pg, Solid and Sybase
  423.  
  424. --skip-delete
  425.   This is a test specific option that is only used when debugging a test.
  426.   This will keep the test tables after the test is run.
  427.  
  428. --skip-test=test1[,test2,...]
  429.   For run-all-programs;  Don\'t execute the named tests
  430.  
  431. --small-test
  432.   This runs some tests with smaller limits to get a faster test.
  433.   Can be used if you just want to verify that the database works, but
  434.   don't have time to run a full test.
  435.  
  436. --small-tables
  437.   This runs some tests that generate big tables with fewer rows.
  438.   This can be used with databases that can\'t handle that many rows
  439.   because of pre-sized partitions.
  440.  
  441. --suffix (Default $opt_suffix)
  442.   The suffix that is added to the database name in the benchmark output
  443.   filename.  This can be used to run the benchmark multiple times with
  444.   different server options without overwritten old files.
  445.   When using --fast the suffix is automaticly set to '_fast'.
  446.  
  447. --time-limit (Default $opt_time_limit)
  448.   How long a test loop is allowed to take, in seconds, before the end result
  449.   is 'estimated'.
  450.  
  451. --use-old-results
  452.   Option to 'run-all-tests' to use the old results from the  '--dir' directory
  453.   instead of running the tests.
  454.  
  455. --user='user_name'
  456.   User name to log into the SQL server.
  457.  
  458. --verbose
  459.   This is a test specific option that is only used when debugging a test.
  460.   Print more information about what is going on.
  461. EOF
  462.   exit(0);
  463. }
  464.  
  465.  
  466.  
  467. ####
  468. #### The end of the base file ...
  469. ####
  470. 1;
  471.